#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_SIZE    20

struct map_node;

struct map_node {
    unsigned int id;
    struct map_node *links[MAX_SIZE];
    unsigned int curr_links;
};

static unsigned int curr_map_nodes = 1;

static struct map_node map_nodes[MAX_SIZE];
static int init = 5;

static void add_one(struct map_node *map_node)
{
    int pos = 0;
    int i = 0;
    unsigned int percent = 0;

    srand((unsigned)time(NULL));
    percent = rand()%curr_map_nodes;
 
    for (i = 0; i < curr_map_nodes; i ++) {
        if (percent >= pos && percent < pos + map_nodes[i].curr_links) {
            map_nodes[i].links[map_nodes[i].curr_links] = map_node;
            map_nodes[i].curr_links ++;
            map_node->curr_links = 1;
            map_node->links[0] = &map_nodes[i];
            curr_map_nodes ++;
#if 0
            printf("percent:%d  pos:%d   total:%d\n", percent, pos, curr_map_nodes);
#endif
            break;
        }
        pos += map_nodes[i].curr_links;
    }
}

int main(int argc, char **argv)
{
    int i, j;
    int A[MAX_SIZE][MAX_SIZE]={0};//临接矩阵声明与初始化

    for (i = 0; i < MAX_SIZE; i ++) {
        memset(&map_nodes[i], 0, sizeof(struct map_node));
        map_nodes[i].id = i + 1;
    }

    for (i = 0; i < init; i ++) {
        map_nodes[i].links[0] = &map_nodes[(i + 1)%init];
        map_nodes[i].curr_links = 1;
        curr_map_nodes ++;
    }

    for (i = init - 1; i < MAX_SIZE; i ++) {
        add_one(&map_nodes[i]);
    }
    // 输出每个节点的属性
    for (i = 0; i < MAX_SIZE; i ++) {
        printf("##### ID:%d links:%d  edges: ", map_nodes[i].id, map_nodes[i].curr_links);
        for (j = 0; j < map_nodes[i].curr_links; j ++) {
            printf("%d ", map_nodes[i].links[j]->id);
            A[map_nodes[i].id-1][map_nodes[i].links[j]->id-1]=1;//有连接的点对应位置置1
            A[map_nodes[i].links[j]->id-1][map_nodes[i].id-1]=1;//有连接的点对应位置置1
        }
        printf("\n");
    }
    //输出邻接矩阵
    for (i = 0; i < MAX_SIZE; i ++) {
        printf("row:%d->",i+1);//输出临接矩阵的行号
        for (j = 0; j < MAX_SIZE; j ++) {
            printf("%d,",A[i][j]);
        }
        printf("\n");
    }

}
